//
//  ServiceMercAddVC.swift
//  kxf
//
//  Created by Elavatine on 2024/10/4.
//

import MCToast

class ServiceMercAddVC: WHBaseViewVC {
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        initUI()
    }
    
    lazy var msgVm: ServiceMercAddMsgVM = {
        let vm = ServiceMercAddMsgVM.init(frame: CGRect.init(x: 0, y: getNavigationBarHeight()+kFitWidth(8), width: 0, height: 0))
        
        return vm
    }()
    lazy var nextBtn : UIButton = {
        let btn = UIButton()
        btn.frame = CGRect.init(x: kFitWidth(30), y: self.msgVm.frame.maxY+kFitWidth(150), width: kFitWidth(316), height: kFitWidth(48))
        btn.setTitle("确定提交", for: .normal)
        btn.setTitleColor(.white, for: .normal)
        btn.setBackgroundImage(UIImage.init(named: "button_theme_bg"), for: .normal)
        btn.layer.cornerRadius = kFitWidth(24)
        btn.clipsToBounds = true
        btn.titleLabel?.font = .systemFont(ofSize: 16, weight: .regular)
        btn.backgroundColor = .clear
        
        btn.addTarget(self, action: #selector(submitAction), for: .touchUpInside)
        
        return btn
    }()
}

extension ServiceMercAddVC{
    @objc func submitAction() {
        if !judgePhoneNumber(phoneNum: msgVm.mercPhoneVm.textField.text){
            MCToast.mc_text("请输入正确的手机号")
            return
        }
        if msgVm.mercNameAllVm.textField.text?.count ?? 0 < 2{
            MCToast.mc_text("请输入代理商名称")
            return
        }
        if msgVm.mercPswVm.textField.text?.count ?? 0 < 6{
            MCToast.mc_text("请输入六位数以上的密码")
            return
        }
        self.sendAddServiceRequest()
    }
}

extension ServiceMercAddVC{
    func initUI() {
        initNavi(titleStr: "服务商信息")
        view.backgroundColor = .COLOR_LIGHT_GREY
        
        view.addSubview(msgVm)
        view.addSubview(nextBtn)
    }
}

extension ServiceMercAddVC{
    func sendAddServiceRequest() {
        let param = ["phone":"\(msgVm.mercPhoneVm.textField.text ?? "")",
                     "agentName":"\(msgVm.mercNameAllVm.textField.text ?? "")",
                     "password":"\(msgVm.mercPswVm.textField.text ?? "")",
                     "inviteAgentPhoneNo":"\(UserModel.shared.agentPhone)"]
        
        WHNetworkUtil.shareManager().POST(urlString: URL_agentRegisterNew, parameters: param as [String : AnyObject]) { responseObject in
            DLLog(message: "URL_agentRegisterNew:\(responseObject)")
            
            self.navigationController?.popViewController(animated: true)
        }
        
    }
}
